package logicaPresentacion;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import logicaNegocio.Aula;
import logicaNegocio.GestionAdministrador;


public class FormularioAula extends JInternalFrame implements ActionListener
{
	    /**
	 * 
	 */
	private static final long serialVersionUID = -7472922003853853840L;
	private JButton btnCancelar,btnGuardar;
    private GestionAdministrador gAd; 
    private String eleccion;     
    private JLabel jlbAforo,jlbCampus;    
    private JTextField jtfAforo; 
    private JComboBox<String>   jcbCampus,jcbAulas;
    private Box boxVCamposVentana;   
    private JPanel panBotones;      
    private ClaseAObservar observable;
    private boolean excepcion = false;
    
    public FormularioAula (GestionAdministrador gAd,String eleccion, ClaseAObservar observable)
    {

        super("Introduzca los datos del aula");
        this.gAd = gAd;
        this.eleccion = eleccion;
        this.observable = observable;
        setSize(400,400);
        Box boxVContenidoVerticalVentana = Box.createVerticalBox();

        Box boxHAforo =  Box.createHorizontalBox();       
        jlbAforo = new JLabel("Aforo (> 50)");   jtfAforo = new JTextField(15);
        boxHAforo.add(Box.createHorizontalStrut(10));
        boxHAforo.add(jlbAforo);
        boxHAforo.add(Box.createHorizontalStrut(11));
        boxHAforo.add(jtfAforo);
        JPanel panAula =  new JPanel(new FlowLayout(FlowLayout.LEFT) );
        panAula.add(boxHAforo);	        
        	        	        
        boxVCamposVentana = Box.createVerticalBox();
        boxVCamposVentana.add(Box.createVerticalStrut(10));
        boxVCamposVentana.add(panAula);
        
        if (eleccion != "Actualizar")
        {
	        Box boxHCampus =  Box.createHorizontalBox();
	        
	        jlbCampus = new JLabel("Campus ");   jcbCampus= new JComboBox<String>();
	        jcbCampus.addActionListener(this);
			for(int i = 0 ; i<gAd.getcampus().size(); i++)
			{
			String nombre = gAd.getCampus(i);
	        jcbCampus.addItem(nombre);
	        }
			boxHCampus.add(Box.createHorizontalStrut(10));
			boxHCampus.add(jlbCampus);
			boxHCampus.add(Box.createHorizontalStrut(40));
			boxHCampus.add(jcbCampus);
	        JPanel panCampus =  new JPanel(new FlowLayout(FlowLayout.LEFT) );
	        panCampus.add(boxHCampus);
	        boxVCamposVentana.add(Box.createVerticalStrut(10));
	        boxVCamposVentana.add(panCampus);
        }

        Box boxHBotones = Box.createHorizontalBox();

        btnGuardar = new JButton("Guardar");
        btnCancelar = new JButton("Cancelar");
        btnGuardar.addActionListener(this);
        btnCancelar.addActionListener(this);
        
        if (eleccion == "Actualizar")
        {
        	ArrayList <Aula> aulas = gAd.getAulas();
        	Box boxHAulas =  Box.createHorizontalBox();
            JLabel jlbAulas;
            
            jlbAulas = new JLabel("Elige el Alumno ");   jcbAulas = new JComboBox<String>();
            jcbAulas.addActionListener(this);
    	
    		for (int i = 0; i<aulas.size();i++) 
    	    {
    	    Aula a = (Aula) aulas.get (i);
            String Nombre =Integer.toString(a.getNumeroAula());
            jcbAulas.addItem(Nombre);
            }
    		
    		boxHAulas.add(Box.createHorizontalStrut(10));
    		boxHAulas.add(jlbAulas);
    		boxHAulas.add(Box.createHorizontalStrut(40));
    		boxHAulas.add(jcbAulas);
            JPanel panAlumnos =  new JPanel(new FlowLayout(FlowLayout.LEFT) );
            panAlumnos.add(boxHAulas);
            
            boxVCamposVentana.add(Box.createVerticalStrut(10));
            boxVCamposVentana.add(panAlumnos);
        }
        
        boxHBotones.add(btnGuardar);
        boxHBotones.add(Box.createHorizontalStrut(20));
        boxHBotones.add(btnCancelar);
        boxHBotones.add(Box.createHorizontalStrut(40));

        panBotones =  new JPanel(new FlowLayout(FlowLayout.RIGHT) );
        panBotones.add(boxHBotones);
        boxVContenidoVerticalVentana.add(Box.createVerticalStrut(30));
        boxVContenidoVerticalVentana.add(boxVCamposVentana);

        boxVContenidoVerticalVentana.add(Box.createVerticalStrut(20));
        boxVContenidoVerticalVentana.add(panBotones);

        add(boxVContenidoVerticalVentana);
        
        setVisible(true);  
    }
	    
	public void actionPerformed(ActionEvent e) 
	{
		if (e.getSource() instanceof JButton) 
		{
			JButton btnPulsado=(JButton)e.getSource();

			if (btnPulsado==btnGuardar) 
			{
				if ("".equals(jtfAforo.getText()))
	            {
					JOptionPane.showMessageDialog(this,"Rellene todos los campos del formulario","Advertencia",JOptionPane.WARNING_MESSAGE);
	            }
				else
				{
					try
	                {
						int tamano = Integer.parseInt(jtfAforo.getText());
	                	tamanoAforo(jtfAforo.getText());
	                }
	                catch(ExcepcionTamano eafo)
	                {
	                	JOptionPane.showMessageDialog(this,"Error de formato en el aforo, deben ser minimo 50","Error",JOptionPane.ERROR_MESSAGE);
	                	excepcion=true;
	                }
					catch(NumberFormatException nfe)
                    {
                    	JOptionPane.showMessageDialog(this,"Error de formato en el aforo, debe ser un numero","Error",JOptionPane.ERROR_MESSAGE);
                    	excepcion=true;
                    }
					if(excepcion ==false)
                    {
						if (eleccion == "Anadir")
						{
		              	   gAd.anadirAula(jtfAforo.getText(),jcbCampus.getSelectedIndex());
		              	   observable.setTabla();
		            	   dispose();
		                }
		            	if (eleccion == "Actualizar")
	            		{
	            			gAd.actualizarAulas( jtfAforo.getText(),jcbAulas.getSelectedIndex());
	            			observable.setTabla();
	            			dispose();
						}
                    }
                    else
                    {
                    	excepcion = false;
                    } 
				}
			}
			else if (btnPulsado==btnCancelar) 
			{
				dispose();
			}						
		}		
	}
	 public void tamanoAforo(String aforo)throws ExcepcionTamano
    {
    	int num = Integer.parseInt(aforo);
    	if(num<50)
    	{
    		throw new ExcepcionTamano();
    	}
    }		
}